<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="item in peoples" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" :rows="3" type="textarea" placeholder="1-300个字符" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="small" @click="onSubmit">确定</el-button>
        <el-button size="small" @click="onClose">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getEmployeeSimple } from '@/api/employees'
import { addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
export default {
  props: {
    existedCodeList: {
      type: Array,
      required: true
    },
    pid: {
      type: String,
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    existedNameList: {
      type: Array,
      required: true
    }
  },
  data() {
    // code的自定义验证规则
    const validCode = (rule, value, callback) => {
      // 把过滤好的数组传过来进行校验
      if (this.isEdit) {
        // 如果当前是编辑，则需要将当前编码从 已经占用的编码列表 中排除在外
        const code = this.existedCodeList.filter(item => item !== this._code)
        code.includes(value) ? callback(`部门编码 ${value}  已经占用`) : callback()
      } else {
        this.existedCodeList.includes(value) ? callback(`部门编码 ${value}  已经占用`) : callback()
      }
    }

    // 这里我只需要做校验想要的数据样
    // 点击添加时要的数据样 ["子级的名字","子级的名字"]
    // 点击编辑时要的数据样 ["同级的名字",'同级的名字'] // 排除掉自己以外的
    const validName = (rule, value, callback) => {
      if (this.isEdit) {
        const code = this.existedNameList.filter(item => item !== this._name)
        code.includes(value) ? callback(`部门名称 ${value}  已经占用`) : callback()
      } else {
        this.existedNameList.includes(value) ? callback(`部门名称 ${value}  已经占用`) : callback()
      }
    }
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      peoples: [],

      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validName, trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: ['blur', 'change'] },
          { validator: validCode, trigger: ['blur', 'change'] }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: ['blur', 'change'] },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created() {
    this.getEmployeeSimpleFn()
    if (this.isEdit) {
      this.getDepartDetailFn()
    }
    console.log(this.existedCodeList)
  },
  methods: {
    // 获取所有员工
    async getEmployeeSimpleFn() {
      const res = await getEmployeeSimple()
      this.peoples = res.data
    },
    // 新增部门方法
    async addDepartmentsFn() {
      await addDepartments({ ...this.form, pid: this.pid })
    },
    // 点击编辑获取到的该信息
    async getDepartDetailFn() {
      const res = await getDepartDetail(this.pid)
      this.form = res.data
      //   console.log(this.form.code)
      this._code = this.form.code
      this._name = this.form.name
    },
    async updateDepartmentsFn() {
      const res = await updateDepartments(this.form)
      console.log(res)
    },
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
        //   if (this.isEdit) {
        //     this.updateDepartmentsFn()
        //   } else {
        //     this.addDepartmentsFn()
        //   }
          this.isEdit ? this.updateDepartmentsFn() : this.addDepartmentsFn()
          // 关闭弹窗
          this.$emit('updata')
        } else {
          alert('滚')
          return false
        }
      })
    },
    // 取消按钮
    onClose() {
      this.$emit('onClose')
    }
  }
}
</script>

<style>

</style>
